package com.zzyl.serve.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;
    @Autowired
    private RedisTemplate redisTemplate;

    private static final String CACHE_KEY_PREFIX = "nursingPlan:listAll";

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        List<NursingProjectPlan> list = nursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        List<NursingProjectPlanVo> nursingProjectPlanVos =  list.stream().map(nursingProjectPlan -> {
            NursingProjectPlanVo nursingProjectPlanVo = new NursingProjectPlanVo();
            nursingProjectPlanVo.setPlanId(Long.valueOf(nursingProjectPlan.getPlanId()+""));
            nursingProjectPlanVo.setProjectId(nursingProjectPlan.getProjectId()+"");
            nursingProjectPlanVo.setId(Long.valueOf(nursingProjectPlan.getId()+""));
            nursingProjectPlanVo.setExecuteTime(nursingProjectPlan.getExecuteTime());
            nursingProjectPlanVo.setExecuteCycle(nursingProjectPlan.getExecuteCycle()+"");
            nursingProjectPlanVo.setExecuteFrequency(Long.valueOf(nursingProjectPlan.getExecuteFrequency()+""));
            return nursingProjectPlanVo;


        }).collect(Collectors.toList());
        NursingPlan nursingPlan = getById(id);
        BeanUtils.copyProperties(nursingPlan,nursingPlanVo);

        nursingPlanVo.setProjectPlans(nursingProjectPlanVos);

        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    public int insertNursingPlan(NursingPlanDto nursingPlanDto)
    {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        this.save(nursingPlan);
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if(!CollectionUtils.isEmpty(projectPlans)){
             projectPlans.forEach(nursingProjectPlan -> {
                nursingProjectPlan.setPlanId(nursingPlan.getId());
             });
        }
        boolean b = nursingProjectPlanService.saveBatch(projectPlans);
        //删除缓存
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return  b?1:0;
    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlanDto nursingPlanDto)
    {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        //1.看条件是否修改计划的项目
        if(!CollectionUtils.isEmpty(projectPlans)){
            //删除旧的计划项目
            nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId,nursingPlan.getId()));
            //批量加入计划项目
            projectPlans.forEach(nursingProjectPlan -> {
                nursingProjectPlan.setPlanId(nursingPlan.getId());
            });
        }
        boolean b = nursingProjectPlanService.saveBatch(projectPlans);

        //删除缓存
        redisTemplate.delete(CACHE_KEY_PREFIX);

        return  b?1:0;
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        boolean falg = removeByIds(Arrays.asList(ids));
        //删除缓存
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return falg ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteNursingPlanById(Long id)
    {
        //删除护理计划与项目的关系
        nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId,id));
        //删除计划
        boolean flag = removeById(id);
        //删除缓存
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return flag? 1 : 0;
    }

    /**
     * 查询所有计划
     * @return
     */
    @Override
    public List<NursingPlan> listAll() {

        //从缓存中获取，命中则返回数据
        List<NursingPlan> all = (List<NursingPlan>) redisTemplate.opsForValue().get(CACHE_KEY_PREFIX);
        if (all!=null){
            return all;
        }
        //缓存中没有则从数据库中获取
        LambdaQueryWrapper<NursingPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NursingPlan::getStatus, 1);
        all = this.list(queryWrapper);
        //将数据放入缓存
        redisTemplate.opsForValue().set(CACHE_KEY_PREFIX,all);
        return all;
    }

}
